package com.fare.taluo.application.impl.system.version;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.system.version.VersionInfoApplication;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.core.domain.system.VersionDetailInfoEntity;
import com.fare.taluo.core.domain.system.VersionInfoEntity;
import com.fare.taluo.vo.core.VersionDetailInfoVO;
import com.fare.taluo.vo.core.VersionInfoVO;

@Transactional
@Service("versionInfoApplication")
public class VersionInfoApplicationImpl implements VersionInfoApplication {

	public VersionInfoVO findVersionInfoVO(Long versionId) {
		VersionInfoEntity  entity = VersionInfoEntity.get(VersionInfoEntity.class, versionId);
		VersionInfoVO vo = this.demain2VO(entity);
		for(VersionDetailInfoEntity e : entity.getDetails()){
			vo.getVersionDetailInfoVOs().add(this.demain2DetailVO(e));
		}
		return vo;
	}

	public List<VersionInfoVO> findversionEntities() {

		List<VersionInfoVO> versionInfoVOs = new ArrayList<VersionInfoVO>();
		VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
		List<VersionInfoEntity> versionInfoEntities = versionInfoEntity.findAllEntities();
		for (VersionInfoEntity e : versionInfoEntities) {
			VersionInfoVO versionInfoVO = new VersionInfoVO();
			versionInfoVO.setVersionId(e.getId());
			versionInfoVO.setVersionNumber(e.getVersionNumber());
			versionInfoVO.setPublisher(e.getPublisher());
			versionInfoVO.setPublishTime(e.getPublishTime());
			versionInfoVO.setCreateTime(e.getCreateTime());
			versionInfoVOs.add(versionInfoVO);
		}

		return versionInfoVOs;
	}

	public void saveOrUpdate(VersionInfoVO vio) {
		VersionInfoEntity entity = null;
		if(vio.getVersionId()!=null){
			entity = VersionInfoEntity.get(VersionInfoEntity.class, vio.getVersionId());
		}else{
			entity = new VersionInfoEntity();
		}
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		entity.setCreater(user.getLocalUserId());
		entity.setVersionNumber(vio.getVersionNumber());
		entity.setPublisher(vio.getPublisher());
		entity.setPublishTime(vio.getPublishTime());
		//先删除
		entity.getDetails().clear();
		//再新增
		if(vio.getVersionDetailInfoVOs() != null && !vio.getVersionDetailInfoVOs().isEmpty()){
			for(VersionDetailInfoVO v : vio.getVersionDetailInfoVOs()){
				entity.getDetails().add(new VersionDetailInfoEntity(
						v.getSeq_Num(),v.getContent(),user.getUserId(), entity));
			}
		}
		entity.save();
	}

	public VersionInfoVO getMaxVersionInfo() {
		VersionInfoEntity entity = new VersionInfoEntity().getMaxVersion();
		VersionInfoVO vo = this.demain2VO(entity);
		for(VersionDetailInfoEntity e : entity.getDetails()){
			vo.getVersionDetailInfoVOs().add(this.demain2DetailVO(e));
		}
		return vo;
	}
	
	private VersionInfoVO demain2VO(VersionInfoEntity entity){
		VersionInfoVO vo = new VersionInfoVO();
		vo.setCreater(entity.getCreater());
		vo.setCreateTime(entity.getCreateTime());
		vo.setPublisher(entity.getPublisher());
		vo.setPublishTime(entity.getPublishTime());
		vo.setVersionId(entity.getId());
		vo.setVersionNumber(entity.getVersionNumber());
		
		return vo;
	}

	private VersionDetailInfoVO demain2DetailVO(VersionDetailInfoEntity entity){
		VersionDetailInfoVO vo = new VersionDetailInfoVO();
		vo.setCreater(entity.getCreater());
		vo.setCreateTime(entity.getCreateTime());
		vo.setContent(entity.getContent());
		vo.setSeq_Num(entity.getSeq_Num());
		vo.setVersion_Detailid(entity.getId());
		
		return vo;
	}
	
	public void delete(Long versionId) {
		VersionInfoEntity entity = VersionInfoEntity.get(
				VersionInfoEntity.class, versionId);
		entity.setFlgDeleted(1);
		entity.saveOrUpdate(entity);
	}

}
